package com.giraffe.device.config;

import com.taosdata.jdbc.TSDBDriver;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @Author ldx
 * @Date 2024/02/22 23:05
 **/
@Data
@Component
@Configuration
public class TdEngineConfig {

    @Value("${spring.tdengine.url:url}")
    private String url;

    @Value("${spring.tdengine.dbName:dbName}")
    private String dbName;

    public Statement tdEngine() throws SQLException {
        String jdbcUrl = url;
        Properties connProps = new Properties();
        connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
        connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
        connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
        connProps.setProperty(TSDBDriver.PROPERTY_KEY_BATCH_LOAD, "true");
        connProps.setProperty("debugFlag", "135");
        Connection conn = DriverManager.getConnection(jdbcUrl, connProps);
        Statement stmt = conn.createStatement();
        return stmt;
    }

    public void tdEngineClose(Statement stmt) throws SQLException {
        Connection con = stmt.getConnection();
        stmt.close();
        con.close();
    }

}
